package cc.web.vaaking.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

public class AjaxTool {
    public static void returnAjax(HttpServletResponse response, AjaxResult ajaxResult) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        try (PrintWriter out = response.getWriter()) {
            out.write(JSON.toJSONString(ajaxResult));
//            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static JSON getJSON(HttpServletRequest request) {
        BufferedReader reader = null;
        JSON obj = null;
        try {
            reader = request.getReader();
            String json ="";
            String tmp=null;
            while ((tmp=reader.readLine())!=null){
                json+=tmp;
            }
            obj = (JSON) JSON.parse(json);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  obj;
    }
}
